/***********************************************************************
 * 	Copyright @ 2011 Zhejiang Yutian Technology Co.,Ltd
 ==================================================================
 Project:        LaYouNet
 FileName:       BaseColumnFragment.java
 Describtion:    文件描述
 Author:         xufei
 Create Date:    2014-7-24 下午4:11:08
 *********************************************************************/
package com.jason.platform.supervision.fragment;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.View;
import com.jason.platform.supervision.base.WeakReferenceHandler;
import com.jason.platform.supervision.task.Task;

import java.io.InputStream;

/**
 * @ClassName: BaseFragment
 * @Description: BaseFragment 基类Fragment
 * @Author: Jason.Zhang zhangyujn1989ok@gmail.com
 * @Date: 2015-7-27 15:39:51
 */
public class BaseFragment extends Fragment {

    public boolean mHasNextPage = false;//是否有分页

    public boolean mIsLoading = true;//当前数据是否在加载

    public boolean mIsStart = true;//是否是第一次调用接口

    public int mCurrentIndex = 0;//当前页码

    Object tag, arg1, arg2;

    public void setTag(Object tag) {
        this.tag = tag;
    }

    public Object getFragmentTag() {
        return tag;
    }

    public void setArg1(Object arg1) {
        this.arg1 = arg1;
    }

    public void setArg2(Object arg2) {
        this.arg2 = arg2;
    }

    private int mNetInitState = NetState.INIT_FIRST;

    public static class NetState {
        public final static int INIT_FIRST = 0;
        public final static int INITING = 1;
        public final static int INIT_SUCCESS = 2;
        public final static int INIT_FAILED = 3;
        public final static int INIT_NO_NETWORK = 4;
    }

    public static class DataState {
        public final static int DATA_FIRST = 0;
        public final static int DATA_LOADING = 1;
        public final static int DATA_REFRESH = 2;
        public final static int DATA_INIT_COMPLETE = 3;
        public final static int DATA_SEARCH = 4;
        public final static int DATA_MORE = 5;
    }

    public int getNetInitState() {
        return mNetInitState;
    }

    public void setNetInitState(int mNetInitState) {
        this.mNetInitState = mNetInitState;
    }


    public void onFrameResume() {
    }

    public void onNetChanged() {
    }

    public void onFrameStop() {
    }


    public void initData(int netState){

    }

    void handleMessage(BaseFragment fragment, Message msg) {

    }

    public void onDataCallback(Task task){}

    ;

    static class MyHandler<T> extends WeakReferenceHandler<T> {

        public MyHandler(T reference) {
            super(reference);
        }

        @Override
        protected void handleMessage(T reference, Message msg) {
            BaseFragment fragment = (BaseFragment) reference;
            fragment.handleMessage(fragment, msg);
        }

    }

    public void onClick(View view) {
    }

    public static Bitmap readBitMap(Context context, int resId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.RGB_565;
        opt.inPurgeable = true;
        opt.inInputShareable = true;
        opt.inSampleSize = computeSampleSize(opt, -1, 128 * 128);  //计算出图片使用的inSampleSize
        opt.inJustDecodeBounds = false;
        //获取资源图片 
        InputStream is = context.getResources().openRawResource(resId);
        return BitmapFactory.decodeStream(is, null, opt);
    }

    public static int computeSampleSize(BitmapFactory.Options options,
                                        int minSideLength, int maxNumOfPixels) {
        int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);

        int roundedSize;
        if (initialSize <= 8) {
            roundedSize = 1;
            while (roundedSize < initialSize) {
                roundedSize <<= 1;
            }
        } else {
            roundedSize = (initialSize + 7) / 8 * 8;
        }

        return roundedSize;
    }

    private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength, int maxNumOfPixels) {
        double w = options.outWidth;
        double h = options.outHeight;

        int lowerBound = (maxNumOfPixels == -1) ? 1 :
                (int) Math.ceil(Math.sqrt(w * h / maxNumOfPixels));
        int upperBound = (minSideLength == -1) ? 128 :
                (int) Math.min(Math.floor(w / minSideLength),
                        Math.floor(h / minSideLength));

        if (upperBound < lowerBound) {
            // return the larger one when there is no overlapping zone.
            return lowerBound;
        }

        if ((maxNumOfPixels == -1) &&
                (minSideLength == -1)) {
            return 1;
        } else if (minSideLength == -1) {
            return lowerBound;
        } else {
            return upperBound;
        }
    }

}
